﻿using System;

using OpenQA.Selenium;

using NUnit.Framework;
using OpenQA.Selenium.Support.UI;

namespace mvcForum.Tests.WebUI {

	[TestFixture]
	public class A002_InstallTest {
		private static IWebDriver driver;

		#region Initialization and cleanup
		[TestFixtureSetUp]
		public static void TestInitialize() {
			driver = TestHelper.GetDriver();
		}

		[TestFixtureTearDown]
		public static void TestCleanup() {
			if (driver != null) {
				driver.Close();
			}
		}
		#endregion

		[Test]
		public void TestInstall() {
			driver.VisitURL("forumadmin/basicinstall");
			//driver.Navigate().GoToUrl(String.Format("{0}forumadmin/basicinstall", TestHelper.SiteBaseURL));

			IWebElement adminCheckbox = driver.FindElement(By.Name("CreateAdmin"));
			Assert.IsNotNull(adminCheckbox);
			adminCheckbox.Click();

			IWebElement usernameBox = driver.FindElement(By.Name("AdminUsername"));
			Assert.IsNotNull(usernameBox);
			usernameBox.Clear();
			usernameBox.SendKeys(Constants.Users.Admin.Username);

			IWebElement emailBox = driver.FindElement(By.Name("AdminEmail"));
			Assert.IsNotNull(emailBox);
			emailBox.Clear();
			emailBox.SendKeys(Constants.Users.Admin.Email);

			IWebElement passwordBox = driver.FindElement(By.Name("AdminPassword"));
			Assert.IsNotNull(passwordBox);
			passwordBox.Clear();
			passwordBox.SendKeys(Constants.Users.Admin.Password);

			IWebElement groupCheckbox = driver.FindElement(By.Name("CreateGroups"));
			Assert.IsNotNull(groupCheckbox);
			groupCheckbox.Click();

			IWebElement forumCheckbox = driver.FindElement(By.Name("CreateSimpleForum"));
			Assert.IsNotNull(forumCheckbox);
			forumCheckbox.Click();

			IWebElement siteURLBox = driver.FindElement(By.Name("SiteURL"));
			Assert.IsNotNull(siteURLBox);
			siteURLBox.Clear();
			siteURLBox.SendKeys(TestConfiguration.SiteBaseURL);

			IWebElement button = driver.FindElement(By.ClassName("btn-primary"));
			Assert.IsNotNull(button);
			button.Click();

			WebDriverWait wait = new WebDriverWait(driver, new TimeSpan(0, 0, 5));
			wait.Until(drv => drv.FindElement(By.Id("install")));

			Assert.AreEqual(String.Format("{0}forumadmin/basicinstall/status", TestConfiguration.SiteBaseURL), driver.Url.ToLower());
			Assert.AreEqual(true, driver.PageSource.Contains("Installation done"));

			driver.LogOff();
		}
	}
}